﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="Database.cs" company="Concept Architecture">
//   (c) 2010 Muhammad Umer Farooq
// </copyright>
// <summary>
//   Defines the Database type.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace Hisaab.Data
{
    using System;
    using System.Collections;
    using System.Data.Objects;
    using Entities;
    using Interfaces;

    /// <summary>
    /// Defines the <seealso cref="Hisaab.Data.Database"/> type.
    /// </summary>
    public sealed class Database : IDatabase, IDisposable
    {
        /// <summary>
        /// Data model to use
        /// </summary>
        private readonly HisaabDataModel _dataModel = new HisaabDataModel();

        /// <summary>
        /// Gets the accounts.
        /// </summary>
        public IObjectSet<Account> Accounts
        {
            get { return _dataModel.Accounts; }
        }

        /// <summary>
        /// Gets the account types.
        /// </summary>
        public IObjectSet<AccountType> AccountTypes
        {
            get { return _dataModel.AccountTypes; }
        }

        /// <summary>
        /// Gets the tags.
        /// </summary>
        public IObjectSet<Tag> Tags
        {
            get { return _dataModel.Tags; }
        }

        /// <summary>
        /// Gets the transactions.
        /// </summary>
        /// <value>The transactions.</value>
        public IObjectSet<Transaction> Transactions
        {
            get { return _dataModel.Transactions; }
        }

        /// <summary>
        /// Gets the vehicles.
        /// </summary>
        /// <value>The vehicles.</value>
        public IObjectSet<Vehicle> Vehicles
        {
            get { return _dataModel.Vehicles; }
        }

        /// <summary>
        /// Gets the vehicle transactions.
        /// </summary>
        /// <value>The vehicle transactions.</value>
        public IObjectSet<VehicleTransaction> VehicleTransactions
        {
            get { return _dataModel.VehicleTransactions; }
        }

        /// <summary>
        /// Gets the users.
        /// </summary>
        public IObjectSet<User> Users
        {
            get { return _dataModel.Users; }
        }

        /// <summary>
        /// Gets the targets.
        /// </summary>
        /// <value>The targets.</value>
        public IObjectSet<Target> Targets
        {
            get { return _dataModel.Targets; }
        }

        /// <summary>
        /// Gets the bills.
        /// </summary>
        /// <value>The bills.</value>
        public IObjectSet<Bill> Bills
        {
            get { return _dataModel.Bills; }
        }

        /// <summary>
        /// Commits the data into database
        /// </summary>
        /// <returns>The number of dirty in the when SaveChanges was called</returns>
        public int SaveChanges()
        {
            return _dataModel.SaveChanges();
        }

        /// <summary>
        /// Updates the System.Data.Objects.ObjectStateManager with store data for specific entities. The order in which entities are refreshed is non-deterministic.
        /// </summary>
        /// <param name="refreshMode">One of the System.Data.Objects.RefreshMode values that specifies which mode to use for refreshing the System.Data.Objects.ObjectStateManager.</param>
        /// <param name="entity">This argument must not be null, and contain System.Data.Objects.DataClasses.IEntity object. Object must be attached to this context.</param>
        public void Refresh(RefreshMode refreshMode, object entity)
        {
            _dataModel.Refresh(refreshMode, entity);
        }

        /// <summary>
        /// Updates the System.Data.Objects.ObjectStateManager with store data for specific entities. The order in which entities are refreshed is non-deterministic.
        /// </summary>
        /// <param name="refreshMode">One of the System.Data.Objects.RefreshMode values that specifies which mode to use for refreshing the System.Data.Objects.ObjectStateManager.</param>
        /// <param name="collection">This argument must not be null, and contain System.Data.Objects.DataClasses.IEntity objects. All System.Data.Objects.DataClasses.IEntity objects must be attached to this context.</param>
        public void Refresh(RefreshMode refreshMode, IEnumerable collection)
        {
            _dataModel.Refresh(refreshMode, collection);
        }

        /// <summary>
        /// Defines a method to release allocated resources.
        /// </summary>
        public void Dispose()
        {
            _dataModel.Dispose();
        }
    }
}